import org.newdawn.slick.*;
import org.newdawn.slick.geom.*;
/**
 * Controller class for the player object. Handles player object creation (and recreation when they die)
 *
 * @author Gerrit Jamerson, CodeMonkeys
 * @version 4/4/13
 */
public class PlayerController
{
    private InputController input;
    private PlayerObject playerObject; //The player object
    private Vector2f velocity; //The velocity of the player object

    /**
     * Constructs the playerController.
     */
    public PlayerController()
    {
        System.out.println("Player Controller created.");

        velocity = new Vector2f(0, 0);
        input = GameController.getInputController();

        setPlayerObject(new PlayerObject());
    } //End velocity

    /**
     * Sets the player object
     * 
     * @pre None
     * @param p The player object
     * @return void
     * @post None
     */
    public void setPlayerObject(PlayerObject p)
    {
        playerObject = p;
    } //End setPlayerObject method

    /**
     * Returns the player object
     * 
     * @pre None
     * @param None
     * @return void
     * @post None
     */
    public PlayerObject getPlayerObject()
    {
        return playerObject;
    } //End player object method

    /**
     * Called each frame. Used to draw the layer.
     *
     * @pre None
     * @param gameContainer The gameContainer
     * @param graphics The Graphics class
     * @return void
     * @post The layer is drawn onto the screen, if needed
     */
    public void render(GameContainer gameContainer, Graphics graphics)
    {
        Vector2f camPos = new Vector2f(playerObject.getPosition());
        camPos = camPos.sub(new Vector2f(Settings.getResolutionX() / 2f, Settings.getResolutionY() / 2f));
        camPos = camPos.add(new Vector2f(playerObject.getBounds().getWidth() / 2, playerObject.getBounds().getHeight() / 2)).negate();
        
        GameController.getGameMode().getLevelController().setCameraPos(camPos);
        
        graphics.drawString("[" + (int)(velocity.x * 100) + ", " + (int)(velocity.y * 100) + " ]", 10, 100);
        Animation curAnimation = playerObject.getCurrentAnimation();
        float temp = (float)Math.sin(((float)(curAnimation.getFrame() + 1.0f) / 6.0f) * (2.0f * Math.PI)) * 10.0f;
        graphics.drawString("" + curAnimation.getFrame() + 1, 10, 120);
        graphics.drawString("" + (curAnimation.getFrame() + 1) / 6.0f, 10, 140);
        float temp2 = (curAnimation.getFrame() + 1.0f) / 6.0f;
        graphics.drawString("" + Math.sin(temp2 * (Math.PI)), 10, 160);
        graphics.drawString("" + temp, 10, 180);
    } //End render method

    /**
     * Called each step. Used to update the layer.
     *
     * @pre None
     * @param gameContainer The gameContainer
     * @param intDelta The delta time between updates
     * @return void
     * @post The layer is drawn onto the screen, if needed
     */
    public void update(GameContainer gameContainer, int intDelta)
    {
        float accelSpeed = 0.04f;
        float maxSpeed = 0.3f;
        boolean up, down, left, right;
        up = input.isKeyDown(Keys.UP);
        down = input.isKeyDown(Keys.DOWN);
        left = input.isKeyDown(Keys.LEFT);
        right = input.isKeyDown(Keys.RIGHT);
        
        velocity.x -= (left ? accelSpeed : 0);
        velocity.x += (right ? accelSpeed : 0);
        velocity.y -= (up ? accelSpeed : 0);
        velocity.y += (down ? accelSpeed : 0);

        if(!(left || right))
        {
            velocity.x = velocity.x / 1.15f;
        } //End if
        
        if(!(up || down))
        {
            velocity.y = velocity.y / 1.15f;
        } //End if

        float x = velocity.x;
        float y = velocity.y;
        if(velocity.length() > maxSpeed)
        {
            y = velocity.normalise().y * maxSpeed;
            x = velocity.normalise().x * maxSpeed;
        } //End if
        velocity.x = x;
        velocity.y = y;
        
        Vector2f xVel = new Vector2f(velocity.x * intDelta, 0);
        Vector2f yVel = new Vector2f(0, velocity.y * intDelta);
        if(playerObject.checkCollidesWith("TileWall", playerObject.getPosition().add(xVel)))
        {
            velocity.x = 0;
        } //End if
        
        
        if(playerObject.checkCollidesWith("TileWall", playerObject.getPosition().add(yVel)))
        {
            velocity.y = 0;
        } //End if
        
        playerObject.setVelocity(velocity);
    } //End update method

    /**
     * Called when a key is pressed.
     * 
     * @pre None
     * @param key The key that was pressed.
     * @return void
     * @post None
     */
    public void keyPressed(Keys key)
    {

    } //End keyPress method

    /**
     * Called when a key is pressed.
     * 
     * @pre None
     * @param key The key that was released.
     * @return void
     * @post None
     */
    public void keyReleased(Keys key)
    {
        
    } //End keyRelease method
} //End class

